#include "order.h"
#include "ui_order.h"

const QString Order::QUERY_ALL_ORDERS = QString("SELECT pedido.id, cliente.id, cliente.nombre AS 'Nombre', cliente.apellido AS 'Apellido', detalle AS 'Detalle', fecha AS 'Fecha', hora AS 'Hora', entregado AS 'Entregado' FROM pedido, cliente WHERE pedido.cliente = cliente.id ORDER BY fecha");
const QString Order::QUERY_FULL_ORDER = QString("SELECT pedido.id, cliente.id, cliente.nombre, cliente.apellido, cliente.calle, cliente.numero, cliente.piso, cliente.departamento, cliente.telefono, cliente.email, detalle, fecha, hora, entregado FROM pedido, cliente WHERE pedido.cliente = cliente.id");

Order::Order(QWidget *parent) : QWidget(parent), ui(new Ui::Order){
    ui->setupUi(this);

    connect(ui->btnSave, SIGNAL(clicked()), this, SLOT(accept()));
    connect(ui->btnCancel, SIGNAL(clicked()), this, SLOT(close()));

    centerOnScreen();
    this->setWindowTitle("Pedidos");
}

void Order::centerOnScreen(){

    this->setWindowFlags(Qt::WindowStaysOnTopHint);

    QRect rect = this->frameGeometry();
    rect.moveCenter(QDesktopWidget().availableGeometry().center());
    this->move(rect.topLeft());
    qApp->processEvents();
}

void Order::getOrders(QSqlQueryModel* model){

    model->clear();
    model->setQuery(QUERY_ALL_ORDERS);
}

void Order::setOrder(QString id){

    _currentOrderId = id;
    loadOrder();
}

void Order::setCustomer(QStringList customerData){

    _currentCustomerId = customerData.at(0);

    ui->lineEditName->setText(customerData.at(1));
    ui->lineEditLastname->setText(customerData.at(2));
    ui->lineEditAddress->setText(customerData.at(3));
    ui->lineEditAddressNumber->setText(customerData.at(4));
    ui->lineEditAddressFloor->setText(customerData.at(5));
    ui->lineEditAddressApartment->setText(customerData.at(6));
    ui->lineEditPhone->setText(customerData.at(7));
    ui->lineEditEmail->setText(customerData.at(8));

    ui->dateEditDate->setDate(QDate::currentDate());
    ui->timeEditTime->setTime(QTime::currentTime());
}

void Order::loadOrder(){

    QSqlQueryModel* model = DBConnection::Instance()->getModel(QUERY_FULL_ORDER + " AND pedido.id = " + _currentOrderId);

    ui->lineEditName->setText(model->record(0).value("nombre").toString());
    ui->lineEditLastname->setText(model->record(0).value("apellido").toString());
    ui->lineEditAddress->setText(model->record(0).value("calle").toString());
    ui->lineEditAddressNumber->setText(model->record(0).value("numero").toString());
    ui->lineEditAddressFloor->setText(model->record(0).value("piso").toString());
    ui->lineEditAddressApartment->setText(model->record(0).value("departamento").toString());
    ui->lineEditPhone->setText(model->record(0).value("telefono").toString());
    ui->lineEditEmail->setText(model->record(0).value("email").toString());

    ui->dateEditDate->setDate(model->record(0).value("fecha").toDate());
    ui->timeEditTime->setTime(model->record(0).value("hora").toTime());
    ui->checkBox->setChecked(model->record(0).value("entregado").toString()!="no");
    ui->textEditDetails->setText(model->record(0).value("detalle").toString());

    qDebug() << QUERY_FULL_ORDER + " AND id = " + _currentOrderId;
    qDebug() <<model->record(0).value("hora").toString();
}

void Order::deleteOrder(QString id){

    QString query = "DELETE FROM pedido WHERE id = " + id;

    DBConnection::Instance()->execQuery(query);
}

void Order::clearForm(){

    ui->lineEditName->setText("");
    ui->lineEditLastname->setText("");
    ui->lineEditAddress->setText("");
    ui->lineEditAddressNumber->setText("");
    ui->lineEditAddressFloor->setText("");
    ui->lineEditAddressApartment->setText("");
    ui->lineEditPhone->setText("");
    ui->lineEditEmail->setText("");

    ui->dateEditDate->setDate(QDate::currentDate());
    ui->timeEditTime->setTime(QTime::currentTime());
    ui->checkBox->setChecked(false);
    ui->textEditDetails->setText("");
}

void Order::accept(){

    QString query;
    QStringList params;

    params << _currentCustomerId << "'" + ui->textEditDetails->toPlainText() + "'"  <<
              "'" + ui->dateEditDate->date().toString("yyyy-MM-dd") + "'" << "'" + ui->timeEditTime->text() + "'"<<
              (ui->checkBox->isChecked()?"'si'":"'no'");

    if (_currentCustomerId != ""){

        query = "INSERT INTO pedido (cliente, detalle, fecha, hora, entregado) VALUES (";

        for (int i=0; i < params.size() -1; i++){
            query = query + params.at(i) + ",";
        }

        query = query + params.at(params.size() -1) + ")";
    }
    else{

        query = "UPDATE pedido SET fecha = " + params.at(2) +
                "," + "hora = " + params.at(3) +
                "," + "entregado = " + params.at(4) +
                "," + "detalle = " + params.at(1) +
                " WHERE id = " + _currentOrderId;
    }

    qDebug() << query;
    DBConnection::Instance()->execQuery(query);
    emit updateOrders();
    close();
}

void Order::close(){

    _currentOrderId = "";
    _currentCustomerId = "";
    clearForm();
    QWidget::close();
}

Order::~Order(){
    delete ui;
}
